package yesjava;

import java.io.*;
import java.util.*;

public class StuMs {
    private File file;
    private List<Student> allStudents = new ArrayList<>();
    private Map<String ,Student> map = new HashMap<>();
    private BufferedReader in = null;
    private PrintWriter out = null;

    private Scanner scanner;

    /**
     * 构造方法
     *
     * @param path
     */
    public StuMs(String path) {
        //设置路径
        file = new File(path);
        //初始化 IO ，保留原来的文件
        initIO(false);
//        加载学生对象
        loadStudents();
        scanner = new Scanner(System.in);
    }

    /**
     * 初始化IO 内容
     *
     * @param isNew
     */
    private void initIO(boolean isNew) {
        try {
            if (this.in != null) {
                in.close();
            }
            if (this.out != null) {
                out.close();
            }
            if (isNew && file.exists()) {
                file.delete();
                file.createNewFile();
            }
            in = new BufferedReader(new FileReader(file));
            out = new PrintWriter(new FileOutputStream(file, !isNew), true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 重新输出到文件（覆盖）
     */
    private void overrideFile() {
        //保证每次都是新的
        this.initIO(true);
        for (Student student : allStudents) {
            out.println(student);
        }
    }

    /**
     * 加载学生对象
     */
    private void loadStudents() {
        //读取文件
        while (true) {
            try {
                String line = in.readLine();
                if (line == null) {
                    break;
                }
                if (line.trim().length() > 0) {
                    String[] split = line.split("\\b\\s\\b");
                    Student student = new Student(split[0], split[1], Integer.parseInt(split[2]));
                    allStudents.add(student);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 添加学生
     *
     * @param student
     * @return
     */
    public boolean add(Student student) {
        Student stuInFile = this.find(student.getStuNo());
        if (student.equals(stuInFile)) {
            return false;
        }
        allStudents.add(student);
//        out.println(student);
        overrideFile();
        return true;
    }

    /**
     * 根据学号删除学生
     *
     * @param stuNO
     * @return
     */
    public boolean remove(String stuNO) {
        boolean success = false;
//        在集合中删除
       for (Iterator<Student> ita = allStudents.iterator(); ita.hasNext(); ) {
            Student student = ita.next();
            if (student.getStuNo().equals(stuNO)) {
                ita.remove();
                success = true;
                break;
            }
        }

//        体现到 文件中
        if (success == true) {
            overrideFile();
        }
        return success;
    }

    /**
     * 根据学号 查找学员
     *
     * @param stuNO
     * @return
     */
    public Student find(String stuNO) {
        Student rs = null;
        for (Student student : allStudents) {
            if (student.getStuNo().equals(stuNO)) {
                rs = student;
                break;
            }
        }
        return rs;
    }


    /**
     * 更改学员
     *
     * @param student
     */
    public void update(Student student) {
        if (this.find(student.getStuNo()) != null) {
            this.remove(student.getStuNo());
            this.add(student);
        }
    }

    private void show(Student student) {
        if (student == null) {
            System.out.println("学员信息不存在");
            return;
        }
        System.out.println("---------------------------------");
        System.out.println("姓名:" + student.getName());
        System.out.println("学号:" + student.getStuNo());
        int sex = student.getSex();
        System.out.println("性别:" + (sex == 0 ? "女" : (sex == 1 ? "男" : "未知")));
        System.out.println("---------------------------------");
    }


    int status = 0;// 0 初始状态  1 查询 2 添加 3 删除 4 更新

    public void start() {
        while (true) {
            switch (status) {
                case 0:
                    System.out.println("请选择您的操作: 1. 查询学生  2.添加学生 3.删除 4.更改学生信息" );
                    int input = Integer.parseInt(scanner.nextLine().trim()); // “1” -->1
                    if(input==88){
                        System.out.println("系统退出！！！！");
                        System.exit(0);
                     /*   break;*/
                    }
                    status = input;
                    break;
                case 1:
                    status = 1;
                    System.out.println("【查询】请输入学号");
                    show(find( scanner.nextLine().trim() ));
                    status = 0;
                    break;
                case 2:
                    status = 2;
                    System.out.println("【添加】请输入姓名");
                    String name = scanner.nextLine();
                    System.out.println("【添加】请输入学号");
                    String stuNo = scanner.nextLine();
                    System.out.println("【添加】请输入性别: 0 女 1男 2 未知");
                    int sex = Integer.parseInt(scanner.nextLine());
                    boolean addSuccess = this.add(new Student(name,stuNo,sex));
                    System.out.println(addSuccess ?"【添加】添加成功":"【添加】学号已存在");
                    status = 0;
                    break;
                case 3:
                    status = 3;
                    System.out.println("【删除】请输入学号");
                    String xuehao = scanner.nextLine().trim();
                    boolean removeSuccess = remove(xuehao);
                    System.out.println(removeSuccess ?"【删除】删除成功":"【删除】学号不存在");
                    status = 0;
                    break;
                case 4:
                    status = 4;
                    System.out.println("【更改】请输入学号");
                    Student student = find(scanner.nextLine().trim());
                    if(student!=null){
                        System.out.println("【更改】请输入姓名");
                        String name1 = scanner.nextLine();
                        student.setName(name1);
                        System.out.println("【更改】请输入性别: 0 女 1男 2 未知");
                        int sex1 = Integer.parseInt(scanner.nextLine());
                        student.setSex(sex1);
                        update(student);
                    }else {
                        System.out.println("【更改】学号不存在，可直接添加");
                    }
                    status = 0;
                    break;
            }
        }

    }
}


